<?php
/*
 * 权限控制
 */
namespace Dms\Controller;
use Think\Controller;

class AuthController extends CommonController
{
	private function searchTree($data,$parentID)
	{
		$tree = array();
		foreach($data as $key => $value)
		{
			if($value['parentID'] == $parentID)
			{
				$value['child'] = $this->searchTree($data,$value['id']);
				$tree[] = $value;
			}
		}
		return $tree;
	}
	
	private function foreachTree($array,$isFirst = 0)
	{
		$table = '';
		if ($isFirst == 1)
		{
			$table .= '<td>【'.$array['title'].'】 <a href="'.U(MODULE_NAME.'/Auth/addGroup',array('parentID'=>1)).'">添加部门</a></td>';
		}
		else if (count($array['child']) < 1 )
		{
		 	return false;
		}	

		$table .= '<td>';
		foreach ($array['child'] as $key => $value ) 
		{
			$table .= '<table>';
			$table .= '<tr>';
			$table .= '<td><ul style="margin: 0px;padding: 0px;"><li>【'.$value['title'].'】 <a href="'.U(MODULE_NAME.'/Auth/addGroup',array('parentID'=>$value['id'])).'">添加部门</a> <a href="'.U(MODULE_NAME.'/Auth/addUser',array('groupID'=>$value['id'])).'">添加员工</a> <a href="'.U(MODULE_NAME.'/Auth/editGroup',array('id'=>$value['id'])).'">编辑</a></li>';
			$userList = M('permission_user')->where(array('groupID'=>$value['id'],'status'=>1))->order('role desc')->select();
			
			foreach ($userList as $key1 => $value1)
			{
				$table .= '<li>'.$value1['position'].'：'.$value1['userName'].' <a href="'.U(MODULE_NAME.'/Auth/editUser',array('id'=>$value1['id'])).'">编辑</a><li>';
			}
			
			$table .= '</td>';
			if (count($array['child']) > 0 )
			{
				$table .=$this->foreachTree($value);
			}
			$table .= '</tr>';
			$table .= '</table>';
		}
		$table .= '</td>';
		
		return $table;
	}
	
    public function index()
    {
    	$data = M('permission_group')->where(array('status'=>1))->select();
    	$data = $this->searchTree($data,0);
    	$tree = '<table class="tablelist"><tr>'.$this->foreachTree($data[0],1).'</tr></table>';

    	$this->tree = $tree;
    	$this->menuID = 7;
    	$this->menuName = '权限分组';
    	$this->display();
    }

    public function addGroup()
    {
    	$parentID = I('get.parentID');
    	$parentRow = M('permission_group')->where(array('id'=>$parentID))->find();
    	
    	if(empty($parentRow) == true)
    	{
    		$this->error('错误操作');
    	}
    	
        if(IS_POST)
        {
        	$data = array();
        	
            $data['title']  = I("post.title");
            $data['parentID']  = $parentID;
            $data['status'] = 1;

            if(empty($data['title']) == true)
            {
            	$this->error('请输入部门名称');
            }
            
            $groupID = M('permission_group')->add($data);
            
            $rule = M('permission_rule')->where(array('groupID'=>$parentID))->find();
            unset($rule['id']);
            $rule['groupID'] = $groupID;
            
            M('permission_rule')->add($rule);
            
            $this->success('添加成功！',U('Home/auth/index'));
            
            exit;
        }
        
        $parentName = $parentRow['title'];
        
        $this->parentID = $parentID;
        $this->parentName = $parentName;
        $this->menuID = 7;
        $this->menuName = '添加部门';
        $this->display();
    }

    public function editGroup()
    {
    	$id = I("get.id");
    	$group = M('permission_group')->where(array('groupID'=>$id))->find($id);

    	if(empty($group)==true)
    	{
    		$this->error('错误操作');
    	}

    	if(IS_POST)
    	{
    		$data = array();
    		$data['title'] = I("post.title");
    		$data['status'] = I("post.status");
    		
    		if(empty($data['title']) == true)
    		{
    			$this->error('请输入部门名称');
    		}
    		
    		M('permission_group')->where(array('id'=>$id))->save($data);
    		
    		$data = array();
    		$action = I("post.rule");
    		$data['rule'] = implode(",", $action);

    		M('permission_rule')->where(array('groupID'=>$id))->save($data);

            $this->success('修改成功！',U('Home/auth/index'));
            
    		exit;
    	}
    	
    	$rule = M('permission_rule')->where(array('groupID'=>$id))->getField('rule');
    	$rule = explode(',',$rule);
    	
    	$node = M('permission_node')->where(array('status'=>1))->select();
    	
    	$nodeArr = array();
    	foreach ($node as $key => $value)
    	{
    		$tmp = explode("/", $value['node']);

    		if(!is_array($nodeArr[$tmp[2]]))
    		{
    			$nodeArr[$tmp[2]] = array();
    		}
    		if(in_array($value['id'],$rule))
    		{
    			$node[$key]['checked'] = ' checked="checked" ';
    		}
    		array_push($nodeArr[$tmp[2]],$node[$key]);
    	}

    	$this->nodeArr = $nodeArr;

    	$this->group = $group;
    	$this->menuID = 7;
    	$this->menuName = '编辑部门';
    	$this->display();
    }
    
    public function addUser()
    {
    	$groupID = I('get.groupID');
    	$group = M('permission_group')->where(array('id'=>$groupID))->find();
    	 
    	if(empty($group) == true)
    	{
    		$this->error('错误操作');
    	}
    	
    	if(IS_POST)
    	{
    		$data = $_POST;
    		$data['groupID'] = $groupID;
    		
    		if(empty($data['username']) == true || empty($data['password']) == true)
    		{
    			$this->error('请填写账号密码');
    		}
    		
    		$User = new \Model\User();
    		$result = $User->register($data);
    		
    		if($result['valid'] === true)
    		{
    			$this->success('添加成功!',U('Home/auth/index'));
    		}
    		else
    		{
    			$this->error($result['msg']);
    		}
    		exit;
    	}
    	
    	$this->group = $group;
    	$this->menuID = 7;
    	$this->menuName = '添加员工';
    	$this->display();
    }
    
    public function editUser()
    {
    	$id = I("get.id");
    	$user = M('permission_user')->where(array('id'=>$id))->find();
    	 
    	if(empty($user) == true)
    	{
    		$this->error('错误操作');
    	}

    	if(IS_POST)
    	{
    		$password = I("post.password");
    		$group = I("post.groupID");
    		$islock = I("post.islock");
    		$mobile = I("post.mobile");
    		
    		$data = array();
    		$data['position'] = I("post.position");
    		$data['groupID'] = I("post.groupID");
    		$data['role'] = I("post.role");
    		$data['mobile'] = I("post.mobile");
    		$data['status'] = I("post.status");
    		
    		M('permission_user')->where(array('id'=>$id))->save($data);
    
    		if(empty($password) == false)
    		{
    			$User = new \Model\User();
    			$User->changePassword($id, $password);
    		}
    		
			$data = array();
			$action = I("post.rule");
			$data['rule'] = implode(",", $action);
    		
			M('permission_rule')->where(array('userID'=>$id))->save($data);

    		$this->success('修改成功！');
    		exit;
    	}
    	 
    	$group = M('permission_group')->where(array('status'=>1))->select();
    
    	foreach ($group as $key => $value)
    	{
    		if($value['id'] == $user['groupID'])
    		{
    			$group[$key]['selected'] = ' selected="selected" ';
    		}
    	}
    	
    	$rule = M('permission_rule')->where(array('userID'=>$id))->getField('rule');
    	$rule = explode(',',$rule);
    	 
    	$node = M('permission_node')->where(array('status'=>1))->select();
    	 
    	$nodeArr = array();
    	foreach ($node as $key => $value)
    	{
    		$tmp = explode("/", $value['node']);
    	
    		if(!is_array($nodeArr[$tmp[2]]))
    		{
    			$nodeArr[$tmp[2]] = array();
    		}
    		if(in_array($value['id'],$rule))
    		{
    			$node[$key]['checked'] = ' checked="checked" ';
    		}
    		array_push($nodeArr[$tmp[2]],$node[$key]);
    	}
    	
    	$this->nodeArr = $nodeArr;

    	$this->group = $group;
    	$this->user = $user;
    	$this->menuID = 7;
    	$this->menuName = '编辑员工';
    	$this->display();
    }
    
    public function editPerson()
    {
        $id = getIdenty();
        
        $user = M('permission_user')->where(array('id'=>$id))->find();
    
        if(empty($user) == true)
        {
            $this->error('错误操作');
        }

        if(IS_POST)
        {
            $password   = I("post.password");
            $mobile     = I("post.mobile");
    
            $data = array();
            $data['mobile']     = $mobile;
    
            M('permission_user')->where(array('id'=>$id))->save($data);
    
            if(empty($password) == false)
            {
                $User = new \Model\User();
                $User->changePassword($id, $password);
            }

            $this->success('修改成功！');
            exit;
        }
        
        $user['groupName'] = getGroupName($user['groupID']);
        
        $this->user = $user;
        $this->menuID = 7;
        $this->menuName = '修改个人资料';
        $this->display();
    }

    public function node()
    {
    	$node = M('permission_node')->where(array('status'=>1))->select();
    	$nodeArr = array();
    	foreach ($node as $key => $value)
    	{
    		$tmp = explode("/", $value['node']);
    	
    		if(!is_array($nodeArr[$tmp[2]]))
    		{
    			$nodeArr[$tmp[2]] = array();
    		}
    		array_push($nodeArr[$tmp[2]],$node[$key]);
    	}

    	$this->nodeArr = $nodeArr;
    	$this->menuID = 6;
    	$this->menuName = '权限列表';
    	$this->display();
    }

    public function addNode()
    {
    	if(IS_POST)
    	{
    		$data = array();
    		$data['title'] = I("post.title");
    		$data['node'] = I("post.node");

    		if(empty($data['title']) == true || empty($data['node']) == true)
    		{
    			$this->error('请确认必填项');
    		}

    		if(M('permission_node')->where(array('node'=>$data['node']))->find() != false)
    		{
    			$this->error('已存在节点');
    		}

    		if(M('permission_node')->where(array('title'=>$data['title']))->find() != false)
    		{
    			$this->error('已存在节点名称');
    		}

    		$data['status'] = 1;

    		M('permission_node')->add($data);

    		$this->success('添加成功',U(MODULE_NAME.'/Auth/node'));
    		exit;
    	}

    	$this->menuID = 6;
    	$this->menuName = '添加节点';
    	
    	$this->display();
    }

    public function editNode()
    {
    	$id = I("get.id");
    	$nodeRow = M('permission_node')->where(array('id'=>$id))->find();

    	if(empty($nodeRow)==true)
    	{
    		$this->error('错误操作');
    	}

    	if(IS_POST)
    	{
    		$title = I("post.title");
    		$node = I("post.node");

    		if(empty($title) == true || empty($node) == true)
    		{
    			$this->error('请确认必填项');
    		}

    		$tmp = M('permission_node')->where(array('node'=>$node))->find();

    		if(empty($tmp) == false && $tmp['id'] != $nodeRow['id'])
    		{
    			$this->error('已存在节点');
    		}

    		$tmp = M('permission_node')->where(array('title'=>$title))->find();

    		if(empty($tmp) == false && $tmp['id'] != $nodeRow['id'])
    		{
    			$this->error('已存在节点名称');
    		}

    		$data = array(
    			'title' => $title,
				'node' => $node,
    		);

    		M('permission_node')->where(array('id'=>$id))->save($data);

    		$this->success('修改成功',U(MODULE_NAME.'/Auth/node'));
    		exit;
    	}

    	$this->node = $nodeRow;
    	$this->menuID = 6;
    	$this->menuName = '编辑节点';
    	$this->display();
    }

    public function deleteNode()
    {
        $id = I("get.id");
    	$nodeRow = M('permission_node')->where(array('id'=>$id))->find();

    	if(empty($nodeRow)==true)
    	{
    		$this->error('错误操作');
    	}

    	M('permission_node')->where(array('id'=>$id))->delete();

    	$this->success('删除成功',U(MODULE_NAME.'/Auth/node'));
    }
}